﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace Windows8Accelerator.Core.Xaml.Controls
{
    [TemplatePart(Name="PasswordBox", Type=typeof(PasswordBox))]
    public class SuperPasswordBox : Control
    {
        public string Password
        {
            get { return (string)GetValue(PasswordProperty); }
            set { SetValue(PasswordProperty, value); }
        }
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.Register("Password", typeof(string), typeof(SuperPasswordBox), new PropertyMetadata(null));

        private PasswordBox PasswordBox;

        public SuperPasswordBox()
        {
            DefaultStyleKey = typeof(SuperPasswordBox);
        }

        protected override void OnApplyTemplate()
        {
            PasswordBox = GetTemplateChild("PasswordBox") as PasswordBox;
            if (PasswordBox != null)
                PasswordBox.PasswordChanged += PasswordBox_PasswordChanged;
        }

        void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            PlaceholderVisibility = string.IsNullOrEmpty(PasswordBox.Password) ? Visibility.Visible : Visibility.Collapsed;
            Password = PasswordBox.Password;
            if (PasswordChangedCommand != null)
                PasswordChangedCommand.Execute(PasswordBox.Password);
        }

        public ICommand PasswordChangedCommand
        {
            get { return (ICommand)GetValue(TextChangedCommandProperty); }
            set { SetValue(TextChangedCommandProperty, value); }
        }
        public static readonly DependencyProperty TextChangedCommandProperty =
            DependencyProperty.Register("TextChangedCommand", typeof(ICommand), typeof(SuperPasswordBox), new PropertyMetadata(null));
       
        public bool IsValid
        {
            get { return (bool)GetValue(IsValidProperty); }
            set { SetValue(IsValidProperty, value); }
        }
        public static readonly DependencyProperty IsValidProperty =
            DependencyProperty.Register("IsValid", typeof(bool), typeof(SuperPasswordBox), new PropertyMetadata(true, IsValidPropertyChanged));

        private static void IsValidPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var SuperPasswordBox = d as SuperPasswordBox;
            SuperPasswordBox.InvalidBorderVisibility = ((bool)e.NewValue) ? Visibility.Collapsed : Visibility.Visible;
        }

        public Visibility InvalidBorderVisibility
        {
            get { return (Visibility)GetValue(InvalidBorderVisibilityProperty); }
            set { SetValue(InvalidBorderVisibilityProperty, value); }
        }
        public static readonly DependencyProperty InvalidBorderVisibilityProperty =
            DependencyProperty.Register("InvalidBorderVisibility", typeof(Visibility), typeof(SuperPasswordBox), new PropertyMetadata(Visibility.Collapsed));

        public string Placeholder
        {
            get { return (string)GetValue(PlaceholderProperty); }
            set { SetValue(PlaceholderProperty, value); }
        }
        public static readonly DependencyProperty PlaceholderProperty =
            DependencyProperty.Register("Placeholder", typeof(string), typeof(SuperPasswordBox), new PropertyMetadata(null));

        public Brush InvalidBorderBrush
        {
            get { return (Brush)GetValue(InvalidBorderBrushProperty); }
            set { SetValue(InvalidBorderBrushProperty, value); }
        }
        public static readonly DependencyProperty InvalidBorderBrushProperty =
            DependencyProperty.Register("InvalidBorderBrush", typeof(Brush), typeof(SuperPasswordBox), new PropertyMetadata(null));

        public Visibility PlaceholderVisibility
        {
            get { return (Visibility)GetValue(PlaceholderVisibilityProperty); }
            set { SetValue(PlaceholderVisibilityProperty, value); }
        }
        public static readonly DependencyProperty PlaceholderVisibilityProperty =
            DependencyProperty.Register("PlaceholderVisibility", typeof(Visibility), typeof(SuperPasswordBox), new PropertyMetadata(Visibility.Collapsed));
   
    }
}
